package LeetCode_201_300;

import java.util.Arrays;

class Solution283 {
    public void moveZeroes(int[] nums) {
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                if (i != j) { // 避免原地覆盖
                    nums[j] = nums[i];
                    nums[i] = 0;
                }
                j++;
            }
        }
    }
}

public class LeetCode283_moveZeroes {
    public static void main(String[] args) {
        Solution283 solution = new Solution283();
        int[] nums1 = new int[] {0,1,0,3,12};
        int[] nums2 = new int[] {0};
        System.out.println(Arrays.toString(nums1));
        solution.moveZeroes(nums1);
        System.out.println(Arrays.toString(nums1));
        System.out.println(Arrays.toString(nums2));
        solution.moveZeroes(nums2);
        System.out.println(Arrays.toString(nums2));
    }
}
